#include<func.h>
//服务端接收客户端发来的文件
//结构体之后放到头文件里去
typedef struct train_s{
    int dataLength;
    char buf[1000];
} train_t;

int recvn(int netFd,void* pstart,int len);

int recvFromClient(char* argv,char*pwd,int netFd){//一次接收完
    train_t t;
    bzero(&t,sizeof(t));
    
    //接收文件名长度
    recvn(netFd, &t.dataLength, sizeof(int));
    //接收文件名
    recvn(netFd, t.buf, t.dataLength);
    //接收方创建一个同名文件
    int fd = open(t.buf, O_RDWR, 0666);
    off_t fileSize;
    bzero(&t,sizeof(t));
    recvn(netFd,&t.dataLength, sizeof(int));
    recvn(netFd,&fileSize,t.dataLength);
    printf("fileSize = %ld\n", fileSize);
    ftruncate(fd,fileSize);
 
    char *p = (char *)mmap(NULL,fileSize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    ERROR_CHECK(p,MAP_FAILED,"mmap");
    recvn(netFd,p,fileSize);
    printf("Upload:100.00%%\n");
    munmap(p,fileSize);
    close(fd);

}

int recvn(int netFd,void* pstart,int len)
{
    int total=0;
    int ret;
    char *p=(char*)pstart;
    while(total<len)
    {
        ret=recv(netFd,p+total,len-total,0);
        total+=ret;//每次接收到的字节数加到total上
    }
    return 0;
}